package com.bz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.bz.common.ConfigUtils;
import com.bz.common.RedisServiceUtil;
import com.bz.dao.ConfigDao;
import com.bz.entity.ConfigEntity;
import com.bz.service.IConfigService;

@Service
public class ConfigServiceImpl implements IConfigService {
    private Logger           log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ConfigDao        configDao;

    @Autowired
    private RedisServiceUtil redisServiceUtil;

    @Override
    public void loadConfig() {
        List<ConfigEntity> clist = configDao.selectList(null);
        if (CollectionUtils.isEmpty(clist)) {
            log.info("加载系统配置为空");
            return;
        }
        Map<String, Map<String, String>> configMap = new HashMap<>();
        for (ConfigEntity c : clist) {
            log.info("加载系统配置-----------type:" + c.getType() + "----------key:" + c.getKey() + "----------value:" + c.getValue());
            redisServiceUtil.hmSet("config:" + c.getType(), c.getType() + ":" + c.getKey(), c.getValue());
            String type = c.getType();
            Map<String, String> tm = new HashMap<>();
            if (configMap.containsKey(type)) {
                tm = configMap.get(type);
            }
            tm.put(c.getKey(), c.getValue());
            configMap.put(type, tm);
        }
        ConfigUtils.configMap = configMap;
    }
}
